package appx.craft.droid.uc;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import appx.craft.droid.R;
import appx.craft.droid.bean.MessageBean;
import appx.craft.droid.ui.HomeScreen;
import appx.craft.droid.utils.Const;
import appx.craft.droid.utils.Log;


public class FragmentMessages extends android.support.v4.app.Fragment implements OnClickListener {
	
	public final String TAG = FragmentMessages.class.getCanonicalName();
	HomeScreen mCurrentActivity;
	ArrayList<MessageBean> arrMessagesList;
	int mMessageNum;
	TextView mMessageContent;
	EditText edit_text_message;
	Button btn_send_message;
	
	public static FragmentMessages newInstance(int Num) {
        final FragmentMessages mFragmentMessages = new FragmentMessages();
        final Bundle args = new Bundle();
        args.putInt(Const.NUM_MESSAGE_VIEWS, Num);
        mFragmentMessages.setArguments(args);
        return mFragmentMessages;
    }
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		Log.debug(TAG,"[ onAttach ] ");
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.debug(TAG,"[ onCreate ] ");
		 mMessageNum = getArguments() != null ? getArguments().getInt(Const.NUM_MESSAGE_VIEWS) : -1;
		 
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.debug(TAG,"[ onCreateView ] ");
		 // Inflate and locate the main ImageView
        final View mView = inflater.inflate(R.layout.fragment_messages, container, false);
        
        mMessageContent = (TextView)mView.findViewById(R.id.textView_message_content);
        mMessageContent.setMovementMethod(new ScrollingMovementMethod());
        
        btn_send_message = (Button)mView.findViewById(R.id.btn_send_message);
        btn_send_message.setOnClickListener(this);
        btn_send_message.setVisibility(View.GONE);
        
        edit_text_message = (EditText)mView.findViewById(R.id.edit_text_message);
        edit_text_message.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
				if(s.toString().length()>0){
					btn_send_message.setVisibility(View.VISIBLE);
				}else if(s.toString().length() == 0){
					btn_send_message.setVisibility(View.GONE);
				}
					
			}
		});
        
        if (HomeScreen.class.isInstance(getActivity())) {
        	mCurrentActivity = (HomeScreen)getActivity();
        	arrMessagesList = mCurrentActivity.takeListofMessages();
        			
		 }
         if(mMessageContent != null){
			 
			 /**
			  * current message bundle
			  */
			 MessageBean mBean$Messages = arrMessagesList.get(mMessageNum);
			 
			 /**
			  * set message content 
			  */
			 StringBuilder mBuilderCntnt = new StringBuilder();
			 mBuilderCntnt.append(mBean$Messages.getmMessage$content());
			 mMessageContent.setText(mBuilderCntnt.toString());
		 }	
        
		return mView;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		 Log.debug(TAG, "[ onActivityCreated ]");
	}
	
	@Override
	public void onStart() {
		super.onStart();
		Log.debug(TAG,"[ onStart ] ");
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
		Log.debug(TAG,"[ onResume ] ");
	}
	
	@Override
	public void onPause() {
		super.onPause();
		Log.debug(TAG,"[ onPause ] ");
	}
	@Override
	public void onStop() {
		super.onStop();
		Log.debug(TAG,"[ onStop ] ");
	}
	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.debug(TAG,"[ onDestroyView ] ");
		mMessageContent.setText(null);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.debug(TAG,"[ onDestroy ] ");
	}
	
	@Override
	public void onDetach() {
		super.onDetach();
		Log.debug(TAG,"[ onDetach ] ");
	}

	@Override
	public void onClick(View v) {
		
		if(v.getId()== R.id.btn_send_message){
			
			MessageBean mNew$BeanMessage = new MessageBean();
			mNew$BeanMessage.setmMessage$Index(String.valueOf(mMessageNum+1));
			mNew$BeanMessage.setmMessage$title("Title");
			mNew$BeanMessage.setmMessage$subtitle("sub title");
			mNew$BeanMessage.setmMessage$content(edit_text_message.getText().toString());
			mCurrentActivity.setMessageInMessageList(mMessageNum,mNew$BeanMessage);
			edit_text_message.setText(null);
			edit_text_message.clearFocus();
		}
		
	}
	
	
}
